<!DOCTYPE HTML>
<html>
	<head>
	<title>次新股</title>
	<link href="css/text.css" rel="stylesheet" type="text/css" />
	<meta charset="utf-8" />
	<style>
		.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}			
		table{
			width:600px; 
			border-collapse:collapse; 
			text-align:center;
		}
		td,th{border:1px solid #696969}
	</style>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
	</head>
	<body>
		<div id="data"></div>
		<script>
			var dt = "";
			var tt = "";
			var gt = "";
			var k = "0";			
			window.onload = function() {				
				var day = new Date();
				gt = day.getTime();
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;
				dt = tt;				
				changeStyle();
				getSource(1);
			}
			
			var dc = 1;
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {					
					dc++;	
					getSource(fs);								
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","24:15")						
				},
				60000);
			})
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function changeStyle() {
				var obj = document.getElementById("towrite");								
				obj.style.left= "0px"; 				
				obj.style.width= "1100px";	
				//obj.style.height= "620px";		
				obj.style.fontSize="12px";					
			}
			
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();
				} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
			
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			var trid = "";
			function changeCss(obj,hid,id){						
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){														
					tlist[i].style.backgroundColor = "#252528"; 					
				}
				trid = hid;
				var tr = document.getElementById(hid);				
				tr.style.backgroundColor = "#483D8B";						
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background="#252528";
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}			
			
			var ts = "";			
			function cDayFunc(dp) 
			{ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			function BookingRmk(date)
			{         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);						
				k = 0;
				if(date1.getTime() < date2.getTime()){			
					k = 1;
					tradeday();
				}								
				getSource(fs);						
			}

			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() - 24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");
					//alert(tt);
					var url = "https://apphis.longhuvip.com/w1/api/index.php?st=1000&Index=0&old=1&Order=1&a=ZhiShuStockList_W8&apiv=w26&Type=6&c=ZhiShuRanking&PlateID=801104&Date=" + tt;
					var xhr = new XMLHttpRequest();	
					xhr.open("GET", url, false);//同步 
					xhr.send(null)																										
					var data = xhr.responseText;
					//document.write(data);
					if(data.indexOf('MYSQL数据库链接出错!') == -1){						
						break;
					}
				}
			}	
			
			var fs = "";
			var text = "";			
			function getSource(p) {					
				fs = p;				
				div = document.getElementById('towrite');				
				var p1 = "<a href='javascript:void(0)' id='px1' onclick='getSource(" + "1" + ")'>" + "开板次新" + "</a>";	
				var p2 = "<a href='javascript:void(0)' id='px2' onclick='getSource(" + "2" + ")'>" + "注册制次新" + "</a>";													
				div.innerHTML = '<font size="4">次新股行情</font><font size="2">（ ' + p1 + p2 + '）</font><input style="width: 120px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" /><br><br>';						
				
				aObj = document.getElementById('px' + p);				
				aObj.style.color = "#FFA07A";
				if(p == 1){
					dm = "801104";
				}else{
					dm = "801065";
				}
				var url1 = "https://apphq.longhuvip.com/w1/api/index.php?Order=1&st=1000&a=ZhiShuStockList_W8&c=ZhiShuRanking&old=1&apiv=w21&Type=6&PlateID=" + dm;
				var url2 = "https://apphis.longhuvip.com/w1/api/index.php?st=1000&Index=0&old=1&Order=1&a=ZhiShuStockList_W8&apiv=w26&Type=6&c=ZhiShuRanking&PlateID=" + dm + "&Date=" + tt;
				
				var url = url1;
				if(k == "1" ){
				  url = url2;
				}
				
				createXmlHttp();
				xmlHttp.onreadystatechange = writeSource;
				xmlHttp.open("GET", url, true);
				xmlHttp.send(null);
				
				//东财新股数据
				if(dc == 1){  //首次
					for(var i = 0; i < 1; i++){	 //循环次数越多，发行日期越靠前，前500对KPL次新已足够
						var url3 = "https://datacenter-web.eastmoney.com/api/data/v1/get?callback=jQuery112308824790082522975_1645943153631&sortColumns=APPLY_DATE,SECURITY_CODE&sortTypes=-1,-1&pageSize=500&pageNumber=" + i + "&reportName=RPTA_APP_IPOAPPLY&columns=SECURITY_CODE,SECURITY_NAME,ISSUE_PRICE,CLOSE_PRICE,LISTING_DATE,ISSUE_NUM,ONLINE_ISSUE_NUM,OFFLINE_EP_OBJECT,INITIAL_MULTIPLE";
						xmlHttp1 = new XMLHttpRequest();
						xmlHttp1.open("GET", url3, false);
						xmlHttp1.send(null);
						var data1 = xmlHttp1.responseText;
						if(data1.indexOf('"message":"返回数据为空"') == -1){
							data1 = data1.replace(/ /g,"").replace(/00:00:00/g,"");
							var ss =  data1.match(/"data":\[(\S*)\],/)[1];
							text += ss + ",";
						}else{
							break;
						}
					}
					//document.write(text);
				}
			}			
			
			//发行价
			function Oprice(s) { 			
				var price = "";
				if(text.indexOf(s) > -1){								
					price = text.split(s)[1].split('"ISSUE_PRICE":')[1].split(',')[0];					
				}else{
					var url = "https://apparticle.longhuvip.com/w1/api/index.php?a=GetCompanyInfo&apiv=w28&c=StockF10Basic&StockID=" + s.replace(/\"/g,"");				
					xmlHttp = new XMLHttpRequest();	
					xmlHttp.open("GET", url, false);  //同步方式
					xmlHttp.send(null);																		
					var data = xmlHttp.responseText
					//document.write(data);
					price = data.match(/\"IsPrice\":\"(\S*)\\u5143\",/)[1];	
				}				
				return price; 
			}
			
			function opzf(num1,num2) {			
				var a = parseFloat(num1); 
				var b = parseFloat(num2);				
				if (isNaN(a) || isNaN(b)) {  				    
				   return false;  
				}				
				var result = ((a/b - 1) * 100).toFixed(2)						
				return result;  				
			}
			
			
			function writeSource() { 
				if (xmlHttp.readyState == 4) {					
					var data = xmlHttp.responseText
					//document.write(data);
					if(k == 0){
						tt = data.match(/"Day":\["(\S*)"\],"Min"/)[1];
						dt = tt;
					}
					
					data = data.replace(/ /g,"").match(/{"list":(\S*)/)[1].split("]]")[0] + "]]";
					//document.write(data);
					data = data.replace(/(\[\").*?(?=\])/g,function(word){ 
						str = word.match(/\[(\S*)/)[1];
						var dm = str.split(',')[0];
						var fxj = Oprice(dm);						
						var xj = str.split(',')[5];
						var xz ="";
						if(text.indexOf(dm) > -1){								
							xz = ',"发行价":' + fxj + ',"上市以来涨幅%":' + opzf(xj,fxj) + ',"CLOSE_PRICE":' + text.split(dm)[1].split('"CLOSE_PRICE":')[1].split('}')[0];
						}else{
							xz = ',"发行价":' + fxj + ',"上市以来涨幅%":' + opzf(xj,fxj) + ',"CLOSE_PRICE":null,"LISTING_DATE":null,"ISSUE_NUM":null,"ONLINE_ISSUE_NUM":null,"OFFLINE_EP_OBJECT":null,"INITIAL_MULTIPLE":null';
						}
						return '{"代码":' + dm + 
						',"名称":' + str.split(',')[1] + 						
						',"现价":' + xj +
						',"涨幅%":' + str.split(',')[6] + 					
						',"涨速%":' + str.split(',')[9]+ 
						',"实际换手%":' + str.split(',')[8]+						
						',"主力净额":' + str.split(',')[13] + 
						',"板块":' + str.split(',')[4] + xz;
					});
					data = data.replace(/\],\{/g,"},{").replace("]]","}]");
					//document.write(data);
					//涨速				
					data = data.replace(/(\"涨速%\":).*?(?=,)/g, function(word){ 				
						str = word.match(/\"涨速%\":(\S*)/)[1]; 					
						return '\"涨速%\":"' + keepTwoDecimal(str) + '"';
					});  	
				
					//换手				
					data = data.replace(/(\"实际换手%\":).*?(?=,)/g, function(word){ 				
						str = word.match(/\"实际换手%\":(\S*)/)[1]; 					
						return '\"实际换手%\":"' + keepTwoDecimal(str) + '"';
					});  
					
					//主力净额				
					data = data.replace(/(\"主力净额\":).*?(?=,)/g, function(word){ 						
					str = word.match(/\"主力净额\":(\S*)/)[1]; 					
					if(keepTwoDecimal(Math.abs(str/100000000)) >= 1){
						return '\"主力净额\":\"' + keepTwoDecimal(str/100000000) + '亿\"';
					}else{
						return '\"主力净额\":\"' + Math.round(str/10000) + '万\"';
					}								
					}); 					
					
					var json = JSON.parse(data);
					
					//document.write(json);
					
					//创建table					
					var table=document.createElement("table");										
					var thead=document.createElement("thead");
					table.appendChild(thead);					
					var tr=document.createElement("tr");
					thead.appendChild(tr);					
					
					var m = 0;
					var n = 0;
					var b = 0;
					var gl = "涨幅";  //高亮
					for(var key in json[0]){  //表头
					//alert(key)
						key = key.replace('CLOSE_PRICE','首日收盘价').replace('ONLINE_ISSUE_NUM','网上发行').replace('LISTING_DATE','上市日期');
						key = key.replace('ISSUE_NUM','发行总数').replace('OFFLINE_EP_OBJECT','报价家数').replace('INITIAL_MULTIPLE','报价倍数');
						var th=document.createElement("th");						
						th.innerHTML=key;						
						tr.appendChild(th);
						if(gl == key.replace("%","")){
							n = m;
							th.style.color = "#FF6633";	
						}
						if(key == "板块"){
							b = m;
							th.style.color = "#FFA500";	
						}
						m++;
					}
										
					var tbody=document.createElement("tbody");					
					table.appendChild(tbody);
					
					//遍历json
					for(var i = 0; i < json.length; i++){					
						var tr=document.createElement("tr");						
						var p = 1;
						var dm = "";
						var mc = "";
						for(var key in json[i]){ //行元素遍历						
							var td=document.createElement("td");							
							
							var rc = json[i][key];
							if(rc == null){
								rc = "-";
							}
							if(p == 1){  //代码列
								dm = rc;
								tr.id = dm;
							}
							if(p == b + 6){	
								if(rc != "-"){
									rc = Math.round(rc);
								}
							}
							if(p == b + 7){
								if(rc != "-"){
									rc = Math.round(rc/10000);
								}
							}								
							td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"" + dm + "\",\"gg\")'>" + rc + "</a>";
							
							if(p == n + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FF6633";
							}
							if(p == b + 1){							
								td.getElementsByTagName("a")[0].style.color = "#FFA500";
							}																
							tr.appendChild(td);							
							p++;
						}														
						tbody.appendChild(tr);																
					}					
					var div = document.getElementById("towrite");									
					div.appendChild(table);
					table.id = "gg";	
								
				}	
				document.getElementById("PriceDate").value = tt;
				
				var table=document.getElementsByTagName("table")[0];
				makeSortable(table);
				hover();
			}
			
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){												
							showCol(n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								if(headtxt.indexOf("名称") == -1 && headtxt.indexOf("板块") == -1 && headtxt.indexOf("日期") == -1){
									if(val1 == "-"){
										val1 = 0;
									}
									if(val2 == "-"){
										val2 = 0;
									}
									val1 = parseFloat(val1);
									val2 = parseFloat(val2);
								}
								
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
							});
							if(flag){
								rows.reverse();
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;
						}
					}(i));
				}
			}
			
			function showCol(iCol){			
				var oTable = document.getElementsByTagName('table')[0];
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){
						oTable.rows[i].cells[j].style.color = "#d2d2d3";						
						oTable.rows[i].cells[7].style.color = "#FFA500";						
					}
					oTable.rows[i].cells[iCol].style.color = "#FF6633";					
				}

				otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					otagsA[i].style.color = "#d2d2d3";
					if(otagsA[i].id == 8){
						otagsA[i].style.color = "#FFA500";								
					}
					if(otagsA[i].id == iCol + 1){
						otagsA[i].style.color = "#FF6633";
					}
				}								
			}
		</script>
		<div id="towrite" class="fixedBox"></div>
	</body>
</html>